package com.ternnetwork.core.service.impl;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.persistence.LockModeType;

import org.springframework.stereotype.Service;

import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.core.dao.UserDao;
import com.ternnetwork.core.dao.UserRoleDao;
import com.ternnetwork.core.entity.Role;
import com.ternnetwork.core.entity.User;
import com.ternnetwork.core.entity.UserRole;
import com.ternnetwork.core.entity.UserRoleId;
import com.ternnetwork.core.service.RoleService;
import com.ternnetwork.core.service.UserRoleService;




@Service("userRoleService")
public class UserRoleServiceImpl implements UserRoleService {
	
	
	@Resource
	private UserRoleDao userRoleDao;
	
	@Resource
	private RoleService roleService;
	
	@Resource
	private UserDao userDao;
	
	@Override
	public void doAdd(UserRole t){
		userRoleDao.persist(t);
	}
	
	@Override
	public void doDelete(UserRole t){
		userRoleDao.delete(t);
	}
	@Override
	public List<UserRole> findByUserId(long userId){
		return userRoleDao.findAll("from UserRole t where t.user.id=?1", userId);
	}
	@Override
	public void doDeleteBatch(List<UserRole> urList){
		for(UserRole t:urList){
			userRoleDao.delete(t);
		}
	}
	@Override
	public int idoDeleteByUserId(long userId){
		return userRoleDao.bulkUpdate("delete from UserRole t where t.user.id=?1", userId);
	}
	@Override
	public void doAddBatch(List<UserRole> urList){
		for(UserRole t:urList){
			userRoleDao.persist(t);
		}
	}
	@Override
	public void doAddUserRoleByRoleIds(User user,String roleIds) {
		 String[] roleIdsArray=roleIds.equals("")?new String[]{}:roleIds.split(",");
		 for(String roleId:roleIdsArray){
				long roleIdL=Long.parseLong(roleId);
				 Role r=roleService.findById(roleIdL);
				 addUserRole(user, r);
		 }
	}
	@Override
	public void doDeleteUserRoleByRoleIds(User user,String roleIds) {
		 String[] roleIdsArray=roleIds.equals("")?new String[]{}:roleIds.split(",");
		 for(String roleId:roleIdsArray){
				long roleIdL=Long.parseLong(roleId);
				 Role r=roleService.findById(roleIdL);
				 deleteUserRole(user, r);
		 }
	}
	
	public void addUserRole(User user, Role r) {
		 UserRoleId id=new UserRoleId();
		 id.setRoleId(r.getId());
		 id.setUserId(user.getId());
		 UserRole ur=userRoleDao.findById(UserRole.class, id);
		 if(ur==null){
			 ur=new UserRole();
			 ur.setId(id);
			 userRoleDao.persist(ur);
		 }
	}
	
	

	public void deleteUserRole(User user, Role r) {
		 UserRoleId id=new UserRoleId();
		 id.setRoleId(r.getId());
		 id.setUserId(user.getId());
		 UserRole ur=userRoleDao.findById(UserRole.class, id, LockModeType.PESSIMISTIC_WRITE);
		 if(ur!=null){
			 user.getUserRoles().remove(ur);
			 userRoleDao.delete(ur);
		 }
	}
	
	@Override
	public BaseResponse idoUpdate(long userId,String roleIds,String deleteRoleIds){
		BaseResponse res=new BaseResponse();
		User t=userDao.findById(User.class, userId,LockModeType.PESSIMISTIC_WRITE);
		if(t==null){
			res.setStatus(0);
			res.setMessage("操作失败，对象不存在！");
			return res;
		}
	    doDeleteUserRoleByRoleIds(t, deleteRoleIds);
	    doAddUserRoleByRoleIds(t, roleIds);
		res.setStatus(0);
		res.setMessage("操作成功！");
		return res;
		
	}
	
	public User deleteUserRoleByRoleIds(String roleIds, User t){
		String[] roleIdsArray=roleIds.equals("")?new String[]{}:roleIds.split(",");
		Iterator it=t.getUserRoles().iterator();
		while(it.hasNext()) {
			 UserRole ur = (UserRole)it.next();
		     for(String roleId:roleIdsArray){
				long roleIdL=Long.parseLong(roleId);
				if(ur.getRole().getId()==roleIdL){
					userRoleDao.delete(ur);
				}
			}
		}
		return t;
	}
	
	public User setUserRoleByRoleIds(String roleIds, User t) {
		Set<UserRole> userRoles=new HashSet<UserRole>();
		String[] roleIdsArray=roleIds.equals("")?new String[]{}:roleIds.split(",");
		for(String roleId:roleIdsArray){
			Role r=new Role();
			r.setId(Long.parseLong(roleId));
			UserRole ur=new UserRole();
			ur.setRole(r);
			ur.setUser(t);
			UserRoleId id=new UserRoleId();
			id.setRoleId(r.getId());
			id.setUserId(t.getId());
			ur.setId(id);
			userRoles.add(ur);
		}
		t.setUserRoles(userRoles);
		return t;
	}

	@Override
	public void doAddUserRole(User user, String roleCode) {
		Role r=roleService.findByCode(roleCode);
		addUserRole(user, r);
	}

	@Override
	public void doDeleteUserRole(User user, String roleCode) {
		Role r=roleService.findByCode(roleCode);
		deleteUserRole(user, r);
	}
		
	

}
